package com.hook3.pianke.Adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hook3.pianke.R;
import com.hook3.pianke.domain.RadioInfo;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by HOME on 2015/12/9.
 */
public class RadioListViewAdapter extends BaseAdapter {
    private ArrayList<RadioInfo.RadioAllList> allRadioList;
    private Activity activity;
    public RadioListViewAdapter(ArrayList<RadioInfo.RadioAllList> allRadioList, Activity activity){
        this.allRadioList = allRadioList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return allRadioList.size();
    }

    @Override
    public RadioInfo.RadioAllList getItem(int position) {
        return allRadioList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(activity, R.layout.item_radio_list,null);
            holder.icon = (ImageView) convertView.findViewById(R.id.iv_radio_list_icon);
            holder.iconNew = (ImageView) convertView.findViewById(R.id.iv_radio_list_new);
            holder.radioName = (TextView) convertView.findViewById(R.id.tv_radio_list_radio_name);
            holder.radioAuthor = (TextView) convertView.findViewById(R.id.tv_radio_list_radio_author);
            holder.radioDes = (TextView) convertView.findViewById(R.id.tv_radio_list_radio_des);
            holder.radioReceivers = (TextView) convertView.findViewById(R.id.tv_radio_list_radio_receivers);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        RadioInfo.RadioAllList radioListInfo = getItem(position);

        //设置广播图片
        ImageOptions imageOptions = new ImageOptions.Builder().build();
        x.image().bind(holder.icon,radioListInfo.coverimg,imageOptions);

        //设置new图片
        if(radioListInfo.isnew){
            holder.iconNew.setVisibility(View.VISIBLE);
        }else{
            holder.iconNew.setVisibility(View.INVISIBLE);
        }

        //设置广播名字
        holder.radioName.setText(radioListInfo.title);

        //设置广播台长名字
        holder.radioAuthor.setText(radioListInfo.userinfo.uname);

        //设置广播描述
        holder.radioDes.setText(radioListInfo.desc);

        //设置广播描述
        holder.radioReceivers.setText(radioListInfo.count);

        return convertView;
    }
    static class ViewHolder {
        ImageView icon;
        ImageView iconNew;
        TextView radioName;
        TextView radioAuthor;
        TextView radioDes;
        TextView radioReceivers;
    }

}
